package com.jsmile.cloud.tradecenter.web.mq;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.jsmile.cloud.tradecenter.api.constants.TradeConstants;
import com.jsmile.cloud.tradecenter.api.enums.CancelTypeEnum;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderCancel;
import com.jsmile.cloud.tradecenter.api.to.OrderStatusChangeTo;
import com.jsmile.cloud.tradecenter.order.OrderApplicationService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 订单超时未付款
 *
 * @author 亮亮
 * @version 1.0.0
 * @date 2017年3月30日 下午3:34:16
 */
@Component
@Slf4j
@AllArgsConstructor
public class OrderOverTimeListener {

    private final OrderApplicationService orderApplicationService;

    @RabbitListener(queuesToDeclare = @Queue(TradeConstants.TradeMq.ORDER_OVERTIME_QUEUE))
    public void handler(OrderStatusChangeTo orderStatusChangeTo) {
        log.info("订单超时未付款：{}", JSON.toJSONString(orderStatusChangeTo));
        ReqOrderCancel reqOrderCancel = new ReqOrderCancel();
        reqOrderCancel.setOrderNo(orderStatusChangeTo.getOrder().getOrderNo());
        reqOrderCancel.setCancelReason("用户超时未付款");
        reqOrderCancel.setCancelType(CancelTypeEnum.USER_CANCEL.getValue());
        try {
            orderApplicationService.cancelOrder(reqOrderCancel);
        } catch (Exception e) {
            log.info("付款的订单无需取消：{}", ExceptionUtils.getStackTrace(e));
        }
    }
}
